package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.commons.exception.LoginPhoneException;
import com.woniuxy.commons.exception.RegisterException;
import com.woniuxy.commons.utile.Result;
import com.woniuxy.controller.dto.NoticeDto;
import com.woniuxy.controller.dto.showContent;
import com.woniuxy.controller.form.AccountForm;
import com.woniuxy.controller.form.NoticeForm;
import com.woniuxy.controller.form.PageForm;
import com.woniuxy.dao.RepairNotice2Dao;
import com.woniuxy.dao.model.Account;
import com.woniuxy.dao.model.RepairNotice2;
import com.woniuxy.service.RepairNotice2Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import netscape.javascript.JSObject;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.validation.Valid;
import java.util.*;

/**
 * <p>
 * 通知公告 前端控制器
 * </p>
 *
 * @author Keep it simple
 * @since 2022-06-28
 */
@Api(tags = "通知公告模块")
@RestController
@RequestMapping("/repairNotice2")
public class RepairNotice2Controller {

    @Resource
    private RepairNotice2Service notice2Service;

    @Resource
    private StringRedisTemplate srt;

    @PostMapping("/addNotice")
    @ApiOperation("新增通知公告")
    private Result addNotice(@Valid @RequestBody NoticeForm noticeForm){
        notice2Service.addNotice(noticeForm);
        return Result.instance().setCode(200).setMessage("新增成功");
    }

    @GetMapping("/getNoticeByTitle")
    @ApiOperation("通过公告标题获取公告的详情")
    @ApiImplicitParam(name = "noticetitle",value = "标题",dataType = "String",required = true)
    private Result getNoticeByTitle(String noticetitle){
        String s = srt.opsForValue().get(noticetitle);
        RepairNotice2 noticeTitle = notice2Service.getOne(new QueryWrapper<RepairNotice2>().eq("noticeTitle", noticetitle));
        //修改公告的浏览次数（最好用redis）
        UpdateWrapper<RepairNotice2> wrapper = new UpdateWrapper<>();
        wrapper.set("browseTimes",noticeTitle.getBrowsetimes()+1);
        wrapper.eq("noticeTitle", noticetitle);
        notice2Service.update(wrapper);
        return Result.instance().setCode(200).setMessage("查询成功").setData(s);
    }

    @GetMapping("/getAllNoticeTitle")
    @ApiOperation("获取所有公告")//可能有问题（应该是获取全部公告的标题用于展示和供用户点击获取其详情）
    private Result getAllNoticeTitle() throws JsonProcessingException {
        Map<Object, Object> title = srt.opsForHash().entries("title");
        Set<Object> obj = title.keySet();
        List<RepairNotice2> list = new ArrayList<>();
        RepairNotice2 repairNotice2 = new RepairNotice2();
        ObjectMapper mapper = new ObjectMapper();
        for (Object o : obj) {
            String  o1 = (String)title.get(o);
            RepairNotice2 repairNotice21 = mapper.readValue(o1, RepairNotice2.class);
            list.add(repairNotice21);
        }
        return Result.instance().setCode(200).setMessage("查询成功").setData(list);
    }


    @PostMapping("/getTiltel")
    @ResponseBody
    public Result getTiltel(@RequestBody PageForm pageForm){
        System.out.println(pageForm);
        System.out.println(".....");
        NoticeDto tiltel = notice2Service.getTiltel(pageForm);
        System.out.println(tiltel);
        return Result.instance().setCode(200).setMessage("成功").setData(tiltel);
    }


    @GetMapping("/test")
    @ResponseBody
    public Result test(String titel){
        System.out.println("titel----------");
        System.out.println(titel);
        RepairNotice2 test = notice2Service.test(titel);
        System.out.println(test);
        return Result.instance().setCode(200).setMessage("成功").setData(test);
    }
}

